package com.itheima.googleplay9.holder;

import android.animation.ValueAnimator;
import android.support.annotation.IntegerRes;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.itheima.googleplay9.R;
import com.itheima.googleplay9.bean.AppInfo;
import com.itheima.googleplay9.global.GooglePlayApp;
import com.itheima.googleplay9.util.LogUtil;

import butterknife.Bind;

/**
 * Created by lxj on 2016/5/27.
 */
public class AppDesHolder extends BaseHolder<AppInfo> implements View.OnClickListener{
    @Bind(R.id.tv_des)
    TextView tvDes;
    @Bind(R.id.tv_author)
    TextView tvAuthor;
    @Bind(R.id.iv_des_arrow)
    ImageView ivDesArrow;


    private ScrollView scrollView;
    public void setScrollView(ScrollView scrollView){
        this.scrollView = scrollView;
    }
    @Override
    protected View initHolderView() {
        View view = View.inflate(GooglePlayApp.context, R.layout.layout_detail_app_des, null);

        view.setOnClickListener(this);
        return view;
    }

    private int minHeight;//5行时候的高度
    private int maxHeight;//全部文本的高度
    @Override
    public void bindData(AppInfo appInfo) {
        tvDes.setText(appInfo.getDes());
        tvAuthor.setText(appInfo.getAuthor());

        //1.让tv_des一开始显示5行
        tvDes.setMaxLines(5);
        //2.获取5行时候的高度
        tvDes.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            /**
             * 该方法是tvDes在父View中执行完layout之后调用
             */
            @Override
            public void onGlobalLayout() {
                tvDes.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //获取5行的高度
                minHeight = tvDes.getHeight();
//
                //3.让tv_des设置显示全部的文本
                tvDes.setMaxLines(Integer.MAX_VALUE);//显示全部的文本
                //再次添加全部布局监听，因为你设置显示全部文本，会引起tvDes进行重新布局
                tvDes.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        tvDes.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        //获取全部的高度
                        maxHeight = tvDes.getHeight();

                        //4.设置回来5行的高度，直接更改其高度
//                        tvDes.setMaxLines(5);
                        ViewGroup.LayoutParams params = tvDes.getLayoutParams();
                        params.height = minHeight;
                        tvDes.setLayoutParams(params);
                    }
                });
            }
        });

    }
    private boolean isExpend = false;//是否是展开的动画
    @Override
    public void onClick(View v) {
        //点击的时候要执行伸展和收缩动画
        ValueAnimator animator =  null;
        if(isExpend){
            //收缩动画
            animator = ValueAnimator.ofInt(maxHeight,minHeight);
        }else {
            //展开动画
            animator = ValueAnimator.ofInt(minHeight,maxHeight);
        }
        //监听动画更新的进度，获取动画的值，实现我们自己的动画逻辑
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
                //将动画的值设置tv_des的高度
                ViewGroup.LayoutParams params = tvDes.getLayoutParams();
                params.height = animatedValue;
                tvDes.setLayoutParams(params);

                //在高度增高的过程中，让scrollView不断地向上滚动
                scrollView.smoothScrollBy(0,1000);//传入一个比较大的值，让ScrollView滚动到底
            }
        });
        animator.setDuration(400);
        animator.start();

        //将标记置为反值
        isExpend = !isExpend;

        //让箭头旋转
        ViewCompat.animate(ivDesArrow).rotationBy(180).setDuration(400).start();
    }
}
